package mya.mes.mdc.logic.tag;

import mya.common.jpa.JPAUtils;
import mya.common.jpa.context.NoLoginLogic;
import mya.common.jpa.context.ServiceContext;
import mya.mes.mdc.model.alarm.MdcAlarmType;
import mya.mes.mdc.model.status.MdcStatusType;
import mya.mes.mdc.model.system.RegularReminders;
import mya.mes.mdc.model.system.TagType;
import mya.mes.mdc.queries.alarm.MdcAlarmTypeQuery;
import mya.mes.mdc.queries.mdc.MdcTagDefQuery;
import mya.mes.mdc.queries.status.MdcStatusTypeQuery;
import mya.mes.mdc.queries.sys.SysQuery;
import mya.mes.mdc.share.vo.plc.MdcAlarmTypeArray;
import mya.mes.mdc.share.vo.plc.MdcStatusTypeArray;
import mya.mes.mdc.share.vo.plc.PlcTagVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PlcTagCache {

    private static PlcTagCache _instance = new PlcTagCache();

    private final MdcAlarmTypeCache alarmTypeCache = new MdcAlarmTypeCache();

    private final MdcStatusTypeCache statusTypeCache = new MdcStatusTypeCache();

    private final HashMap<String, TagType> tagTypes = new HashMap<String, TagType>();

    private final List<RegularReminders> regularReminderses = new ArrayList<RegularReminders>();


    private PlcTagCache() {

    }

    public static PlcTagCache getInstance() {
        return _instance;
    }


    public void updateCache() throws Exception {
        NoLoginLogic logic = new NoLoginLogic(JPAUtils.DEFAULT_TENANT_ID) {
            @Override
            protected Object process(ServiceContext context) throws Exception {
                updateCache(context);
                return null;
            }
        };

        logic.callLogic();
    }

    public MdcAlarmTypeArray getMdcAlarmType(PlcTagVo vo) {
        return alarmTypeCache.get(vo);
    }


    public MdcStatusTypeArray getMdcStatusType(PlcTagVo vo) {
        return statusTypeCache.get(vo);
    }

    public synchronized void updateCache(ServiceContext context) throws Exception {

        List<MdcAlarmType> alarmTypeList = MdcAlarmTypeQuery.loadAll(context);
        List<MdcStatusType> statusTypeList = MdcStatusTypeQuery.loadAll(context);
        alarmTypeCache.update(alarmTypeList);
        statusTypeCache.update(statusTypeList);
    }

    //初始化提醒信息
    public void initRegularReminders() throws Exception {
        NoLoginLogic logic = new NoLoginLogic(JPAUtils.DEFAULT_TENANT_ID) {
            @Override
            protected Object process(ServiceContext context) throws Exception {
                regularReminderses.clear();
                List<RegularReminders> regularReminders = SysQuery.queryAll(context);
                if (regularReminders != null && regularReminders.size() > 0) {
                    regularReminderses.addAll(regularReminders);
                }
                return null;
            }
        };
        logic.callLogic();
    }

    public void initTagType() throws Exception {
        NoLoginLogic logic = new NoLoginLogic(JPAUtils.DEFAULT_TENANT_ID) {
            @Override
            protected Object process(ServiceContext context) throws Exception {
                tagTypes.clear();
                List<TagType> tagTypeList = MdcTagDefQuery.queryTagType(context);
                if (tagTypeList != null && tagTypeList.size() > 0) {
                    for (TagType tagType : tagTypeList) {
                        tagTypes.put(tagType.getTagName(), tagType);
                    }
                }
                return null;
            }
        };
        logic.callLogic();
    }

    public HashMap<String, TagType> getTagTypes() {
        return tagTypes;
    }

    public List<RegularReminders> getRegularReminderses() {
        return regularReminderses;
    }
}
